package org.jxc.web.action;

import java.util.Date;
import java.util.List;

import org.jxc.domain.ProductionOrder;
import org.jxc.domain.ProductionProduct;
import org.jxc.service.ProductionOrderService;
import org.jxc.service.ProductionProductService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;
import org.team4.exception.CRUDException;
import org.team4.util.LoginBean;
import org.team4.web.action.GenericAction;

@Controller
@Scope("prototype")
public class ProductionOrderAction extends GenericAction<ProductionOrder> {
	private static final long serialVersionUID = -4665378081525232678L;
	@Autowired
	private ProductionOrderService service;
	@Autowired
	private ProductionProductService productService;
	
	private ProductionOrder object ;
	
	private List<ProductionProduct> plist;
	
	public String listPagination() throws Exception {
		page = service.listPagination(page);
		return SUCCESS;
	}
	
	public String toSave(){
		return SUCCESS;
	}
	
	public String save() throws CRUDException{
		object.setCreateTime(new Date());
		object.setCreator(LoginBean.getLoginBean().getUser());
		object.setAuditor(LoginBean.getLoginBean().getUser().getDepartment()
				.getManager());
		service.save(object);
		if(plist != null) {
			for (int i = 0; i < plist.size(); i++) {
				ProductionProduct temp = plist.get(i);
				temp.setProductionOrder(object);
				productService.save(temp);
			}
		}
		return SUCCESS;
	}

	public String toUpdate() throws CRUDException {
		object = service.findById(object.getId());
		plist = productService.getListByProductionOrderId(object.getId());
		return SUCCESS;
	}
	
	public String update() throws CRUDException{
		service.update(object);
		if(plist != null) {
			for (int i = 0; i < plist.size(); i++) {
				productService.update(plist.get(i));
			}
		}
		return SUCCESS;
	}
	
	public String view() throws CRUDException {
		object = service.findById(object.getId());
		plist = productService.getListByProductionOrderId(object.getId());
		return SUCCESS;
	}

	public String check() throws CRUDException {
		String auditState = object.getAuditState();
		object = service.findById(object.getId());
		object.setAuditState(auditState);
		service.update(object);
		return SUCCESS;
	}
	
	public String delete() throws CRUDException {
		object = service.findById(object.getId());
		object.setDeleteFlag("1");
		service.update(object);
		return SUCCESS;
	}

	public ProductionOrder getObject() {
		return object;
	}

	public void setObject(ProductionOrder object) {
		this.object = object;
	}

	public List<ProductionProduct> getPlist() {
		return plist;
	}

	public void setPlist(List<ProductionProduct> plist) {
		this.plist = plist;
	}
}
